//
//  configViewController.m
//  tcpoctest
//
//  Created by gump on 10/24/12.
//  Copyright (c) 2012 gump. All rights reserved.
//

#import "configViewController.h"
#import "WFLabel_blackShadow.h"
#import "ViewManager.h"
#import "PublicFun.h"
#import "RegexKitLite.h"
#import "DataControl.h"
#import "SDSegmentedControl.h"

@interface configViewController ()

@end

@implementation configViewController

- (id)init
{
    self = [super init];
    if (self) {
        mailField = nil;
        psField = nil;
        repeatField = nil;
        iWaitCount = 0;
        iViewState = VLT_NULL;
    }
    return self;
}

-(void)initUI
{
    self.view.backgroundColor = [UIColor clearColor];
    
    bgScrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    bgScrollView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:bgScrollView];
    //[bgScrollView release];
    
    UITapGestureRecognizer *singleTouch = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyClick)];
    singleTouch.delegate = self;
    [bgScrollView addGestureRecognizer:singleTouch];
    //[singleTouch release];
    
    [self createTitle];
    self.mtitle.text = [NSString stringWithFormat:@"注册%@",T_PROJECTNAME];
    
    UIBarButtonItem *backItem = [self createBarBackButtonItemWithTitle:@"返回" target:self action:@selector(backButtonPressed)];
    self.navItem.leftBarButtonItem = backItem;
    //[backItem release];
    
    UIBarButtonItem *nextItem = [self createBarButtonItemWithTitle:@"下一步" target:self action:@selector(nextButtonPressed)];
    self.navItem.rightBarButtonItem = nextItem;
    //[nextItem release];
    
    NSArray *segmentedArray = [[NSArray alloc]initWithObjects:@"注册账号",@"个人资料",@"头像",nil];
    SDSegmentedControl *stepControl = [[SDSegmentedControl alloc]initWithItems:segmentedArray];
    //[segmentedArray release];
    stepControl.frame = CGRectMake(0.0, 44.0, 320.0, 44.0);
    stepControl.userInteractionEnabled = NO;
    stepControl.selectedSegmentIndex = 0;
    [bgScrollView addSubview:stepControl];
    //[stepControl release];
    
    inputBg = [[UIImageView alloc]initWithFrame:CGRectMake(11.0, 100.0, 298.0, 170.0)];
    inputBg.image = [UIImage imageNamed:@"accountInputBg.png"];
    inputBg.userInteractionEnabled = YES;
    [bgScrollView addSubview:inputBg];
    //[inputBg release];
    
    //input 
    mailbgv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:I_INPUTBG]];
    mailbgv.frame = CGRectMake(5.5, 10.0, 287.0, 43.0);
    mailbgv.userInteractionEnabled = YES;
    [inputBg addSubview:mailbgv];
    //[mailbgv release];
    
    maillabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 43)];
    maillabel.textColor = [UIColor colorWithRed:(102.0/255.0) green:(102.0/255.0) blue:(102.0/255.0) alpha:1.0];
    maillabel.font = CENTER_CELL_CONTENT_FONT;
    maillabel.backgroundColor = [UIColor clearColor];
    maillabel.textAlignment = NSTextAlignmentLeft;
    maillabel.text = T_MAIL;
    
    mailField = [[UITextField alloc]initWithFrame:CGRectMake(20, 0, 250, 43)];
    mailField.tag = 0;
    mailField.backgroundColor = [UIColor clearColor];
    mailField.leftView = maillabel;
    mailField.leftViewMode = UITextFieldViewModeAlways;
    mailField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    mailField.font = CENTER_CELL_CONTENT_FONT;
    mailField.textColor = [UIColor blackColor];
    mailField.placeholder = @"请输入7位以上数字和字母";
    mailField.returnKeyType = UIReturnKeyNext;
    mailField.delegate = self;
    [mailbgv addSubview:mailField];
    
    psbgv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:I_INPUTBG]];
    psbgv.frame = CGRectMake(5.5, 63.0, 287.0, 43.0);
    psbgv.userInteractionEnabled = YES;
    [inputBg addSubview:psbgv];
    
    pslabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 43)];
    pslabel.textColor = [UIColor colorWithRed:(102.0/255.0) green:(102.0/255.0) blue:(102.0/255.0) alpha:1.0];
    pslabel.font = CENTER_CELL_CONTENT_FONT;
    pslabel.backgroundColor = [UIColor clearColor];
    pslabel.textAlignment = NSTextAlignmentLeft;
    pslabel.text = T_PS;
    
    psField = [[UITextField alloc]initWithFrame:CGRectMake(20, 0, 250, 43)];
    psField.tag = 1;
    psField.backgroundColor = [UIColor clearColor];
    psField.leftView = pslabel;
    psField.leftViewMode = UITextFieldViewModeAlways;
    psField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    psField.font = CENTER_CELL_CONTENT_FONT;
    psField.textColor = [UIColor blackColor];
    psField.placeholder = T_PIPS;
    psField.returnKeyType = UIReturnKeyNext;
    psField.delegate = self;
    psField.secureTextEntry = YES;
    [psbgv addSubview:psField];
    
    repeatbgv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:I_INPUTBG]];
    repeatbgv.frame = CGRectMake(5.5, 116.0, 287.0, 43.0);
    repeatbgv.userInteractionEnabled = YES;
    [inputBg addSubview:repeatbgv];
    
    repeatlabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 43)];
    repeatlabel.textColor = [UIColor colorWithRed:(102.0/255.0) green:(102.0/255.0) blue:(102.0/255.0) alpha:1.0];
    repeatlabel.font = CENTER_CELL_CONTENT_FONT;
    repeatlabel.backgroundColor = [UIColor clearColor];
    repeatlabel.textAlignment = NSTextAlignmentLeft;
    repeatlabel.text = @"确认:";
    
    repeatField = [[UITextField alloc]initWithFrame:CGRectMake(20, 0, 250, 43)];
    repeatField.tag = 2;
    repeatField.backgroundColor = [UIColor clearColor];
    repeatField.leftView = repeatlabel;
    repeatField.leftViewMode = UITextFieldViewModeAlways;
    repeatField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    repeatField.font = CENTER_CELL_CONTENT_FONT;
    repeatField.textColor = [UIColor blackColor];
    repeatField.placeholder = @"请再次输入密码";
    repeatField.returnKeyType = UIReturnKeyDone;
    repeatField.delegate = self;
    repeatField.secureTextEntry = YES;
    [repeatbgv addSubview:repeatField];
    
}

-(BOOL)InputCheck
{
    BOOL res = NO;
    
    do
    {
        
        if (mailField == nil)
        {
            break;
        }
        else if([mailField.text length] == 0)
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                             message:@"账户不能为空,请输入账户"
                                                            delegate:self
                                                   cancelButtonTitle:@"确认"
                                                   otherButtonTitles:nil];
            [alert show];
            
            break;
        }
        else if(![mailField.text isMatchedByRegex:@"\\b(^[A-Za-z0-9]{7,32})\\b"])
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                            message:@"账户输入错误,账户由7位以上数字和字母组成"
                                                           delegate:self
                                                  cancelButtonTitle:@"确认"
                                                  otherButtonTitles:nil];
            [alert show];
            
            break;
        }
        else if([mailField.text isMatchedByRegex:@"\\b(^\\d+$)\\b"])
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                            message:@"账户输入错误,账户不可以是纯数字"
                                                           delegate:self
                                                  cancelButtonTitle:@"确认"
                                                  otherButtonTitles:nil];
            [alert show];
            
            break;
        }
        else if([psField.text length] == 0)
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                            message:@"密码不能为空,请输入密码"
                                                           delegate:self
                                                  cancelButtonTitle:@"确认"
                                                  otherButtonTitles:nil];
            [alert show];
            
            break;
        }
        else if(![psField .text isMatchedByRegex:@"\\b([a-zA-Z0-9]{6,32})\\b"])
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                            message:@"密码输入错误,密码由6位以上数字或字母组成"
                                                           delegate:self
                                                  cancelButtonTitle:@"确认"
                                                  otherButtonTitles:nil];
            [alert show];
            
            break;
        }
        else if([repeatField.text length] == 0)
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                            message:@"重复密码不能为空,请输入重复密码"
                                                           delegate:self
                                                  cancelButtonTitle:@"确认"
                                                  otherButtonTitles:nil];
            [alert show];
            
            break;
        }
        else if(![psField.text isEqualToString:repeatField.text])
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                            message:@"重复密码输入错误,重复密码需与密码相同"
                                                           delegate:self
                                                  cancelButtonTitle:@"确认"
                                                  otherButtonTitles:nil];
            [alert show];
            
            break;

        }
        
        
        [DataControl shareControl].personDS.uinfo.mail = mailField.text;
        
        NSString *ps = md5(psField.text);
        //NSString *ps = psField.text;
        [DataControl shareControl].personDS.uinfo.ps = ps;
        
        res = true;

    } while (false);
       
    return res;
}

#pragma mark -button click-
-(void)backButtonPressed
{
    [[ViewManager sharedViewManager] backView:self];
}

-(void)nextButtonPressed
{
    if ([self InputCheck])
    {
        [self hideKeyClick];
        [DataControl shareControl].personDS.strcurRegisterMail = [DataControl shareControl].personDS.uinfo.mail;
        [[DataControl shareControl].LCDS addLogicCommand:LC_CAR];
        
        [[ViewManager sharedViewManager] showSuccessProgressHUDWithStatus:@"验证用户名..."
                                                                 withView:self.view];
        iWaitCount = 0;
        iViewState = VLT_WAIT;
    };
}



#pragma mark -textfield delegate-

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    [bgScrollView setContentOffset:CGPointMake(0, 50) animated:YES];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    switch (textField.tag) {
        case 0:
            [mailField resignFirstResponder];
            [psField becomeFirstResponder];
            break;
        case 1:
            [psField resignFirstResponder];
            [repeatField becomeFirstResponder];
            break;
        default:
            [bgScrollView setContentOffset:CGPointMake(0.0, 0.0) animated:YES];
            [repeatField resignFirstResponder];
            break;
    }
    
    return YES;
}

#pragma mark -private fun-
-(void)hideKeyClick
{
    if ([mailField isFirstResponder] == YES)
    {
        [mailField resignFirstResponder];
    }
    else if([psField isFirstResponder] == YES)
    {
        [psField resignFirstResponder];
    }
    else if([repeatField isFirstResponder] == YES)
    {
        [repeatField resignFirstResponder];
    }
    [bgScrollView setContentOffset:CGPointMake(0.0, 0.0) animated:YES];
}


-(void)registUserExist
{
    if (iViewState == VLT_WAIT)
    {
        [[ViewManager sharedViewManager] dismissProgressHUD];
        iViewState = VLT_NULL;
        iWaitCount = 0;
        
        [[ViewManager sharedViewManager] showSuccessProgressHUDWithStatus:@"帐户已存在了耶"
                                                                 withView:self.view];
    }
}

-(void)registUserSuccess
{
    if (iViewState == VLT_WAIT)
    {
        [[ViewManager sharedViewManager] dismissProgressHUD];
        iViewState = VLT_NULL;
        iWaitCount = 0;
        
        [[ViewManager sharedViewManager] showView:VI_CONFIGTWO
                                 withIsRemoveCurV:false
                                    withDirection:VD_RIGHT];
    }
}

-(void)viewWillHide
{
    [[ViewManager sharedViewManager] dismissProgressHUD];
}

-(void)clearUI
{
    mailField.text = @"";
    psField.text = @"";
    repeatField.text = @"";
}

-(void)UICustomTick
{
    switch (iViewState) {
        case VLT_WAIT:
        {
            if (iWaitCount > 150)
            {
                iViewState = VLT_NULL;
                [[ViewManager sharedViewManager] dismissProgressHUD];
                iWaitCount = 0;
            }
            else
            {
                ++iWaitCount;
            }
        }
            break;
            
        default:
            break;
    }
}

@end
